package com.hsy.baoinbao.modules.order.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.astuetz.PagerSlidingTabStrip;
import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.order.adapter.ReceivingAllAdapter;
import com.hsy.baoinbao.modules.order.interfaces.IFragmentUpdateListener;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hsy on 2016/3/23.
 */
public class FragmentReceiving extends DPBaseFragment {
    private PagerSlidingTabStrip tabs;
    private ViewPager pager;
    private MyPagerAdapter adapter;
    private DPBaseFragment[] fragments = {
            new FragmentReceivingUndone(),
            new FragmentReceivingDone(),
            new FragmentReceivingAll(),
            new FragmentReceivingIntoGarage()
    };

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_receiving;
    }

    private boolean mHasLoadedOnce = false;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (this.isVisible()) {
            // we check that the fragment is becoming visible
            if (isVisibleToUser && !mHasLoadedOnce) {

                // async http request here
                mHasLoadedOnce = true;
            }
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        tabs = F(R.id.tabs);
        pager = F(R.id.pager);
        adapter = new MyPagerAdapter(getChildFragmentManager());
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(adapter);

        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        pager.setPageMargin(pageMargin);

        tabs.setViewPager(pager);

        tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((IFragmentUpdateListener) fragments[position]).onFragmentUpdate();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        fragments[0].setAttchedActivity(getAttchedActivity());
        fragments[1].setAttchedActivity(getAttchedActivity());
        fragments[2].setAttchedActivity(getAttchedActivity());
        fragments[3].setAttchedActivity(getAttchedActivity());

        Bundle args = getArguments();
        if (null != args) {
            pager.setCurrentItem(args.getInt(Consts.TO_SHOW_FRAGMENT));
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }


    public class MyPagerAdapter extends FragmentPagerAdapter {
        private final String[] TITLES = {"未完成", "已完成", "已取消", "入库"};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        public int getItemPosition(Object obj) {
            return super.getItemPosition(obj);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        DPLogUtil.d("onActivityResult requestCode=" + requestCode + ",resultCode=" + resultCode);
        if (1 == requestCode && 1 == resultCode) {
//            pager.setCurrentItem(2);
        }
    }
}
